<template>
<div style="display: flex; justify-content: center">
  <el-form ref="roleForm" :rules="rules" :model="form" label-width="auto" style="max-width: 600px">
    <el-form-item label="账号" prop="sn">
      <el-input v-model="form.sn" placeholder="请输入用户账号"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="pwd">
      <el-input v-model="form.pwd" placeholder="请输入密码" ></el-input>
    </el-form-item>
    <el-form-item label="爱好" prop="like">
      <el-select v-model="form.like">
        <el-option v-for="(item, index) in likeList" :key="index" :label="item.name" :value="item.value"/>
      </el-select>
    </el-form-item>

    <el-button @click="submit">提交</el-button>
  </el-form>
</div>
</template>

<script setup lang="ts">
import {ref,reactive} from "vue";
const form = reactive({
  sn: '',
  pwd: '',
  like: []
})

const changePwd = (rule,value, callback) => {
  if(value === ''){
    callback(new Error("请输入密码"))
  } else if (value.length > 10) {
    callback(new Error("密码长度不能大于10位"))
  } else {
    callback()
  }
}

const roleForm = ref(null)

const likeList = ref([
  {name: '篮球', value: 'basket'},
  {name: '游戏', value: 'game'},
])
const rules = reactive({
  sn: [
    { required: true, message: '请输入账号', trigger: 'blur' }
  ],
  pwd: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { validator: changePwd, trigger: 'blur' },
    { pattern: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+$/,
        message: '必须包含大小写字母和数字', trigger: 'blur' }
  ]
})

const submit = () => {
  roleForm.value.validate((valid) => {
    if (valid) {
      console.log("成功")
      console.log(form)
    } else {
      console.log("失败")
    }
  })

}
</script>

<style scoped>

</style>